<template>
  <div class="role-container">
    <div class="left-wrapper">
      <div
        v-for="(item ,index) in roleList"
        :key="item.roleId"
        class="role-item"
        :class="{active: index === activeIndex}"
        @click="changeRole(index,item.roleId)"
      >
        <div class="role-info">
          <svg-icon :icon-class="index === activeIndex ? 'user-active' : 'user'" class="icon" />
          {{ item.roleName }}
        </div>
        <div class="more">
          <svg-icon
            name="more"
            icon-class="more"
          />
        </div>
      </div>
      <el-button class="addBtn" size="mini">添加角色</el-button>
    </div>
    <div class="right-wrapper">
      <!-- tree 要求必须转入符合规则的树状结构 [{title:"" children:[{}]},{}]
      后端会直接返回规则的树状结构. 返回的格式不能直接用（处理成树形结构）
      tree默认的识别字段 标题  lable字段 子元素   children 字段  通过props参数做自定义
      {
        children：'childrenlist'
        lable: 'title'
      }
      -->
      <el-tabs v-model="activeName">
        <el-tab-pane label="功能权限" name="tree">
          <div class="tree-wrapper">
            <div v-for="item in treeList" :key="item.id" class="tree-item">
              <!-- title -->
              <div class="tree-title"> {{ item.title }} </div>
              <!-- ！树形结构 -->
              <el-tree
                ref="tree"
                :data="item.children"
                :props="{label: 'title'}"
                show-checkbox
                default-expand-all
                node-key="id"
              />
            </div>
          </div>
        </el-tab-pane>
        <el-tab-pane label="成员列表(100)" name="member">成员管理</el-tab-pane>
      </el-tabs>

    </div>
  </div>
</template>

<script>
import { getRoleListAPI, getTreeListAPI, getRoleDetailAPI } from '@/api/role'
// 添加disabled
// function addDisabled(treeList) {
//   treeList.forEach(item => {
//     item.disabled = true
//     if (item.children) {
//       addDisabled(item.children)
//     }
//   })
// }
export default {
  name: 'Role',
  data() {
    return {
      roleList: [],
      activeIndex: 0,
      treeList: [],
      activeName: 'tree'
    }
  },
  async  mounted() {
    await this.getRoleList()
    await this.getTreeList()
    this.setTreeByPerms(this.roleList[0].roleId)
  },
  methods: {
    async  getRoleList() {
      const res = await getRoleListAPI()
      this.roleList = res.data
      console.log(this.roleList)
    },
    async  setTreeByPerms(roleId) {
      const res = await getRoleDetailAPI(roleId)
      console.log(this.$refs.tree)
      // 获取tree组建的实例对象
      this.$refs.tree.forEach((treeInstance, index) => {
        treeInstance.setCheckedKeys(res.data.perms[index])
      })
    },
    async  changeRole(idx, roleId) {
      // console.log(idx)
      this.activeIndex = idx
      this.setTreeByPerms(roleId)
    },
    addDisabled(treeList) {
      treeList.forEach(item => {
        item.disabled = true
        if (item.children) {
          this.addDisabled(item.children)
        }
      })
    },
    async  getTreeList() {
      const res = await getTreeListAPI()
      this.treeList = res.data
      this.addDisabled(this.treeList)
    }

  }
}
</script>

<style scoped lang="scss">
.role-container {
  display: flex;
  font-size: 14px;
  background-color: #fff;
  padding: 20px;
  .left-wrapper {
    width: 200px;
    border-right: 1px solid #e4e7ec;
    padding: 4px;
    text-align: center;

    .role-item {
      position: relative;
      height: 56px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0 20px;
      cursor: pointer;

      &.active {
        background: #edf5ff;
        color: #4770ff;
      }
    }

    .role-info {
      display: flex;
      align-items: center;

      .icon {
        margin-right: 10px;
      }
    }

    .more {
      display: flex;
      align-items: center;
    }
    .addBtn {
      width: 100%;
      margin-top: 20px;
    }
  }

  .right-wrapper {
    flex: 1;

    .tree-wrapper {
      display: flex;
      justify-content: space-between;

      .tree-item {
        flex: 1;
        border-right: 1px solid #e4e7ec;
        padding: 0px 4px;
        text-align: center;
        .tree-title {
          background: #f5f7fa;
          text-align: center;
          padding: 10px 0;
          margin-bottom: 12px;
        }
      }
    }

    ::v-deep .el-tabs__nav-wrap {
      padding-left: 20px;
    }

    .user-wrapper {
      padding: 20px;
    }
  }
}
</style>
